# device.py

import tkinter as tk

def draw_static_elements(canvas):
    # 设置字体
    font_en = ('Times New Roman', 12, 'italic')
    font_ch = ('KaiTi', 12, )

    #绘制静态元素
    # 注意，这一版每个元素的x坐标都减去了200
    canvas.create_line(75, 225, 125, 225)
    canvas.create_line(75, 275, 125, 275)
    canvas.create_line(125, 150, 125, 225)
    canvas.create_line(125, 275, 125, 350)
    canvas.create_line(125, 225, 125, 275, width=3, dash=4)
    canvas.create_oval(20, 245, 30, 255, fill='black')
    canvas.create_line(25, 250, 75, 225)
    canvas.create_line(25, 250, 75, 225 + 50 / 3)
    canvas.create_line(25, 250, 75, 225 + 100 / 3)
    canvas.create_line(25, 250, 75, 275)
    canvas.create_oval(65, 220, 85, 280, width=3)
    canvas.create_line(75, 150, 75, 350, dash=4)
    canvas.create_line(75, 225 + 50 / 3, 125, 225 + 50 / 3)
    canvas.create_line(75, 225 + 100 / 3, 125, 225 + 100 / 3)
    canvas.create_line(120, 220 + 50 / 3, 125, 225 + 50 / 3)
    canvas.create_line(120, 230 + 50 / 3, 125, 225 + 50 / 3)
    canvas.create_line(120, 220 + 100 / 3, 125, 225 + 100 / 3)
    canvas.create_line(120, 230 + 100 / 3, 125, 225 + 100 / 3)
    canvas.create_line(120, 220, 125, 225)
    canvas.create_line(120, 230, 125, 225)
    canvas.create_line(120, 270, 125, 275)
    canvas.create_line(120, 280, 125, 275)
    canvas.create_text(25, 284, text='光源', font=font_ch)
    canvas.create_text(20, 304, text='波长', font=font_ch)
    canvas.create_text(42, 304, text='λ', font=font_en)
    canvas.create_text(125, 360, text='掩模板', font=font_ch)
    canvas.create_line(125, 345, 130, 340)
    canvas.create_line(125, 345, 130, 350)
    canvas.create_line(225, 345, 220, 340)
    canvas.create_line(225, 345, 220, 350)
    canvas.create_line(225, 345, 230, 340)
    canvas.create_line(225, 345, 230, 350)
    canvas.create_line(325, 345, 320, 340)
    canvas.create_line(325, 345, 320, 350)
    canvas.create_line(325, 345, 330, 340)
    canvas.create_line(325, 345, 330, 350)
    canvas.create_line(425, 345, 420, 340)
    canvas.create_line(425, 345, 420, 350)
    canvas.create_line(425, 345, 430, 340)
    canvas.create_line(425, 345, 430, 350)
    canvas.create_line(525, 345, 520, 340)
    canvas.create_line(525, 345, 520, 350)
    canvas.create_line(125, 345, 170, 345)
    canvas.create_line(180, 345, 225, 345)
    canvas.create_line(225, 345, 270, 345)
    canvas.create_line(280, 345, 325, 345)
    canvas.create_line(325, 345, 370, 345)
    canvas.create_line(380, 345, 425, 345)
    canvas.create_line(425, 345, 470, 345)
    canvas.create_line(480, 345, 525, 345)
    canvas.create_text(175, 345, text='f', font=font_en)
    canvas.create_text(275, 345, text='f', font=font_en)
    canvas.create_text(375, 345, text='f', font=font_en)
    canvas.create_text(475, 345, text='f', font=font_en)
    canvas.create_text(325, 510, text='透镜组', font=font_ch)
    canvas.create_text(525, 360, text='像屏', font=font_ch)
    canvas.create_text(468, 300, text='浸没折射率', font=font_ch)
    canvas.create_text(515, 300, text='n', font=font_en)
    canvas.create_line(225, 0, 225, 500, dash=4)
    canvas.create_line(425, 0, 425, 500, dash=4)
    canvas.create_line(325, 0, 325, 500, dash=4)
    canvas.create_line(125, 225, 225, 225)
    canvas.create_line(125, 275, 225, 275)
    canvas.create_line(225, 225, 425, 275)
    canvas.create_line(225, 275, 425, 225)
    canvas.create_line(525, 225, 425, 225)
    canvas.create_line(525, 275, 425, 275)
    canvas.create_line(525, 150, 525, 350)

def draw_dynamic_elements(canvas, a, m):
    # 绘制动态元素
    # 注意，每个元素的x坐标都减去了200，横纵再以600进行缩放为相对单位
    canvas.create_oval(210, 225 - a, 240, 275 + a, width=3, tags="dynamic")
    canvas.create_oval(410, 225 - a, 440, 275 + a, width=3, tags="dynamic")
    canvas.create_line(125, 225, 225, 225 - a, tags="dynamic")
    canvas.create_line(125, 275, 225, 275 - a, tags="dynamic")
    canvas.create_line(125, 225, 225, 225 + a, tags="dynamic")
    canvas.create_line(125, 275, 225, 275 + a, tags="dynamic")
    canvas.create_line(225, 225 - a, 425, 275 - a, tags="dynamic")
    canvas.create_line(225, 275 - a, 425, 225 - a, tags="dynamic")
    canvas.create_line(225, 225 + a, 425, 275 + a, tags="dynamic")
    canvas.create_line(225, 275 + a, 425, 225 + a, tags="dynamic")
    canvas.create_line(525, 225, 425, 225 - a, tags="dynamic")
    canvas.create_line(525, 275, 425, 275 - a, tags="dynamic")
    canvas.create_line(525, 225, 425, 225 + a, tags="dynamic")
    canvas.create_line(525, 275, 425, 275 + a, tags="dynamic")
    canvas.create_line(525, 225, 525, 275, dash=4, width=3*m-0.5, tags="dynamic")

def update_dynamic_elements(device_canvas, sin_value, n_value):
    b = float(sin_value)
    a = (100 * b) / ((1 - b * b) ** 0.5)
    m = float(n_value)
    
    # 删除具有标签"dynamic"的元素
    device_canvas.delete("dynamic")
    
    # 根据a和m的值绘制动态元素
    draw_dynamic_elements(device_canvas, a, m)


# 绘制网格，方便调试代码时定位坐标
def draw_grid(canvas, line_distance):
    # 获取画布的宽度和高度
    width = canvas.winfo_reqwidth()
    height = canvas.winfo_reqheight()
    # 绘制垂直线和垂直坐标
    for i in range(0, width, line_distance):
        canvas.create_line(i, 0, i, height, fill="#d3d3d3")
        canvas.create_text(i, 0, anchor='n', text=str(i))
    # 绘制水平线和水平坐标
    for i in range(0, height, line_distance):
        canvas.create_line(0, i, width, i, fill="#d3d3d3")
        canvas.create_text(0, i, anchor='w', text=str(i))



# 装置示意图函数调用示例
if __name__ == '__main__':
    root = tk.Tk()
    root.title('装置示意图')
    
    # 先定下窗口的宽度和高度
    window_width = 600
    window_height = 600

    # 获取用户屏幕尺寸，计算屏幕的中心位置
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    position_top = int((screen_height - window_height) / 2)
    position_right = int((screen_width - window_width) / 2)

    # 设置窗口的位置
    root.geometry(f"{window_width}x{window_height}+{position_right}+{position_top}")

    # 设置窗口置顶
    root.attributes('-topmost', True)

    control_frame = tk.Frame(root, width=600, height=600)
    control_frame.pack()
    device_canvas = tk.Canvas(control_frame, width=600, height=600)
    device_canvas.pack()
    draw_grid(device_canvas, 50)
    draw_static_elements(device_canvas)
    draw_dynamic_elements(device_canvas, 10/(0.99**0.5), 1.0)
    root.mainloop()